using System.IO;
using NFreeMarker.Core;

namespace NFreeMarker.Template
{
    public static class TemplateExceptionHandler
    {
        /**
          * This is a TemplateExceptionHandler which simply skips errors. It does nothing
          * to handle the event. Note that the exception is still logged in any case, before
          * being passed to the handler.
          */
        public static readonly ITemplateExceptionHandler IgnoreHandler = new IgnoreHandlerImpl();

        private class IgnoreHandlerImpl : ITemplateExceptionHandler
        {
            public void HandleTemplateException(TemplateException te, Environment env, TextWriter output)
            {
            }
        }

        /**
          * This is a TemplateExceptionHandler that simply rethrows the exception.
          * Note that the exception is logged before being rethrown.
          */
        public static readonly ITemplateExceptionHandler RethrowHandler = new RethrowHandlerImpl();

        private class RethrowHandlerImpl : ITemplateExceptionHandler
        {
            public void HandleTemplateException(TemplateException te, Environment env, TextWriter output)
            {
                throw te;
            }
        }

        /**
          * This is a TemplateExceptionHandler used when you develop the templates. This handler
          * outputs the stack trace information to the client and then rethrows the exception.
          */
        public static readonly ITemplateExceptionHandler DebugHandler = new DebugHandlerImpl();

        private class DebugHandlerImpl : ITemplateExceptionHandler
        {
            public void HandleTemplateException(TemplateException te, Environment env, TextWriter output)
            {
                te.PrintStackTrace(output);
                output.Flush();
                throw te;
            }
        }

        /**
          * This is a TemplateExceptionHandler used when you develop HTML templates. This handler
          * outputs the stack trace information to the client and then rethrows the exception, and
          * surrounds it with tags to make the error message readable with the browser.
          */
        public static readonly ITemplateExceptionHandler HtmlDebugHandler = new HtmlDebugHandlerImpl();

        private class HtmlDebugHandlerImpl : ITemplateExceptionHandler
        {
            public void HandleTemplateException(TemplateException te, Environment env, TextWriter output)
            {
                output.WriteLine("<!-- FREEMARKER ERROR MESSAGE STARTS HERE -->"
                                 + "<script language=javascript>//\"></script>"
                                 + "<script language=javascript>//\'></script>"
                                 + "<script language=javascript>//\"></script>"
                                 + "<script language=javascript>//\'></script>"
                                 + "</title></xmp></script></noscript></style></object>"
                                 + "</head></pre></table>"
                                 + "</form></table></table></table></a></u></i></b>"
                                 + "<div align=left "
                                 + "style='background-color:#FFFF00; color:#FF0000; "
                                 + "display:block; border-top:double; padding:2pt; "
                                 + "font-size:medium; font-family:Arial,sans-serif; "
                                 + "font-style: normal; font-variant: normal; "
                                 + "font-weight: normal; text-decoration: none; "
                                 + "text-transform: none'>"
                                 + "<b style='font-size:medium'>FreeMarker template error!</b>"
                                 + "<pre><xmp>");
                te.PrintStackTrace(output);
                output.WriteLine("</xmp></pre></div></html>");
                output.Flush();
                throw te;
            }
        }
    }
}